//get state-level enrollment-weighted average costs

//location
forval y = 2005 / 2014{
	clear all
	import delimited "$data/IPEDS_late/location/hd`y'_data_stata.csv", clear
	keep unitid fips sector hloffer
	gen year = `y'
	save "$temp/iped_data_location_`y'", replace
}

//enrollment
forval y = 2005 / 2014{
	clear all
	import delimited "$data/IPEDS_late/enrollment/ef`y'd_data_stata.csv", clear
	keep unitid ugentern
	gen year = `y'
	save "$temp/iped_data_enrollment_`y'", replace
}

//student charges
forval y = 2005/2014{
	clear all
	import delimited "$data/IPEDS_late/student_charges/ic`y'_ay_data_stata.csv", clear
	
	//tuition, books, room and board
	keep unitid chg2ay3 chg3ay3 chg4ay3 chg5ay3
	ren chg2 tuition_instate
	ren chg3 tuition_outstate
	ren chg4 books
	ren chg5 roomboard
	gen year = `y'

	save "$temp/iped_data_charges_`y'", replace
}

forval y = 2005/2014{
	use "$temp/iped_data_location_`y'"
	merge 1:1 unitid using "$temp/iped_data_enrollment_`y'", keep(match) nogen
	merge 1:1 unitid using "$temp/iped_data_charges_`y'", keep(match) nogen
	compress
	save "$temp/iped_all_`y'", replace
}

clear
forval y = 2005/2014{
	append using "$temp/iped_all_`y'"
}


save "$temp/iped_all", replace


import delimited "$data/IPEDS_late/sfa/sfa0809_rv_data_stata.csv", clear
keep unitid gis4a12 gis4a22 gis4a32 gis4a42 gis4a52
ren gis4a12 grant_030
ren gis4a22 grant_3048
ren gis4a32 grant_4875
ren gis4a42 grant_75110
ren gis4a52 grant_110p
gen year = 2009
save "$temp/ipeds_grants", replace

//breakup: community, public, bad (for profit or 2-year) private, good private (4-year, NFP)
use "$temp/iped_all", clear
merge 1:1 unitid year using "$temp/ipeds_grants", keep(match) nogen
keep if year>=2008 & year<=2011
drop if ugentern == . | ugentern == 0 //zero or missing enrollment

//category
gen cat = .
replace cat = 1 if sector == 4 //public 2-year
replace cat = 2 if sector == 1 //public 4-year
replace cat = 3 if sector == 3 | sector == 5 | sector == 6 | sector == 8 | sector == 9 //for-profit or <4 year private
replace cat = 4 if sector == 2 //good private
drop if sector == 7 //<2 year public
drop sector

//in-state and out-state costs
drop if tuition_instate == . | tuition_outstate == .
gen cost_instate = tuition_instate + books + roomboard
gen cost_outstate = tuition_outstate + books + roomboard
//drop tuition* books roomboard


//deflation
merge m:1 year using "$data/GDP/gdp_pce_deflator", keep(match) nogen
replace cost_instate = cost_instate/deflator * 100
replace cost_outstate = cost_outstate/deflator * 100
replace tuition_instate = tuition_instate/deflator * 100
replace tuition_outstate = tuition_outstate/deflator * 100
replace cost_instate = cost_instate/40000
replace cost_outstate = cost_outstate/40000


ds grant*
foreach var in `r(varlist)'{
	replace `var' = `var'/deflator * 100
	replace `var' = `var'/40000
	replace `var' = `var' * 4
}


su grant* [fw=ugentern] if cat == 1
su grant* [fw=ugentern] if cat == 2
su grant* [fw=ugentern] if cat == 3
su grant* [fw=ugentern] if cat == 4



su cost_instate [w = ugentern] if cat == 1
local r1 = `r(mean)' 

su cost_outstate [w = ugentern] if cat == 1
local r2 = `r(mean)' 
local cost_1 = (0.915 * `r1' + (1-0.915) * `r2') * 4


su cost_instate [w = ugentern] if cat == 2
local r1 = `r(mean)' 

su cost_outstate [w = ugentern] if cat == 2
local r2 = `r(mean)'
local cost_2 = (0.86 * `r1' + (1-0.86) * `r2') * 4


su cost_outstate [w =ugentern]
di `r(mean)' * 4

//2.91
//2.485

su cost_outstate [w = ugentern] if cat == 3
local cost_3 = `r(mean)' * 4
su cost_outstate [w = ugentern] if cat == 4
local cost_4 = `r(mean)' * 4


di "`cost_1'"
di "`cost_2'"
di "`cost_3'"
di "`cost_4'"



//deflate
su cost* [fw = ugentern] if year == 2007 & cat==3
su cost* [fw = ugentern] if year == 2012 & cat==3

su tuition* if year==2007 & cat == 1
su tuition* if year==2012 & cat == 1


//get public costs
preserve
keep if cat == 1 | cat == 2 //publics
collapse (mean) cost* [fw = ugentern], by(fips year cat)
tempfile public
save `public'
restore

//private costs
keep if cat == 3 | cat == 4
gen cost = cost_instate
drop cost_instate cost_outstate
collapse (mean) cost [w = ugentern], by(cat year)
sort year cat
gen cost_private_good = cost[_n+1] if year[_n] == year[_n+1]
drop if cat == 4
ren cost cost_private_bad
drop cat

merge 1:m year using `public', keep(match) nogen
order year fips cat cost_in cost_out cost_priv*
sort year fips cat
save "$temp/state_college_costs", replace





***********